package manage

import (
	"github.com/zeromicro/go-zero/rest/httpx"
	"net/http"
)

type Body struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func ResponseHandler(w http.ResponseWriter, resp interface{}, err error) {
	if err != nil {
		httpx.OkJson(w, ErrorHandler(err))
	} else {
		httpx.OkJson(w, Body{
			Code: OK.Code,
			Msg:  OK.Msg,
			Data: resp,
		})
	}
}

func Response(w http.ResponseWriter, code int, msg string, data interface{}) {
	httpx.OkJson(w, Body{
		Code: code,
		Msg:  msg,
		Data: data,
	})
}

// Success 成功请求
func Success(w http.ResponseWriter, data interface{}) {
	Response(w, http.StatusOK, "操作成功", data)
}

// Fail 失败请求
func Fail(w http.ResponseWriter, msg string) {
	Response(w, http.StatusInternalServerError, msg, nil)
}
